<?php include("includes/session.php"); ?>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $lang['PAGE_TITLE_RESOURCES']; ?></title>
<?php include('includes/categories.php');
if(isset($_GET['type']) || isset($_GET['category']) || isset($_GET['text']) || isset($_GET['country']))
{
	$search = "SELECT * FROM ".TBL_PLACES." WHERE ";
	$prev = false;
	if(isset($_GET['type']))
	{
		$search .= "type='".$_GET['type']."' ";
		$prev = true;
	}
	if(isset($_GET['category']))
	{
		if($prev)
			$search .= "AND ";
		$search .= "category='".$_GET['category']."' ";
		$prev = true;
	}
	if(isset($_GET['text']))
	{
		if($prev)
			$search .= "AND ";
		$search .= "(title LIKE '%".$_GET['text']."%' OR description LIKE '%".$_GET['text']."%') ";
		$prev = true;
	}
	if(isset($_GET['country']))
	{
		if($prev)
			$search .= "AND ";
		$search .= "countryCode='".$_GET['country']."' ";
		$prev = true;
	}
	$search .= "ORDER BY timestamp DESC";
	$res = $database->query($search);
	$num_res = mysql_num_rows($res);
	
	$start = 0;
	$maxResources = 30;
	if(isset($_GET['start']))
	{
		$start = $_GET['start'];
	}
}
else
{
	$res = $database->query("SELECT * FROM ".TBL_PLACES." ORDER BY timestamp DESC");
	$num_res = mysql_num_rows($res);
	
	$start = 0;
	$maxResources = 30;
	if(isset($_GET['start']))
	{
		$start = $_GET['start'];
	}
}
?>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo GOOGLE_KEY; ?>" type="text/javascript">
</script>
<script type="text/javascript">
var map;
function initialize()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(0, 0), 0);
		map.setUIToDefault();
		map.setMapType(G_SATELLITE_MAP);
		map.setZoom(1);
		
		<?php
		if($num_rows>0)
		{
			for($i=$start;$i<$num_res && $i<$start+$maxResources;$i++)
			{
				$name = mysql_result($res,$i,"title");
				$user = mysql_result($res,$i,"username");
				$latitude = mysql_result($res,$i,"latitude");
				$longitude = mysql_result($res,$i,"longitude");
				if($latitude)
					echo "addMarker($latitude, $longitude, '$name', '$user');";
			}
		}
		?>
	}
}
function addMarker(lat, long, name, user)
{
	if (GBrowserIsCompatible())
	{
		var marker = new GMarker(new GLatLng(lat, long));
		GEvent.addListener(marker,"mouseover",
			new Function("document.getElementById(\"map_name\").innerHTML = \""+name+"\";"));
		GEvent.addListener(marker,"click",
			new Function("window.location = \"profile.php?user="+user+"\";"));
		map.addOverlay(marker);
	}
}
</script>
<?php include("header.php"); ?>
	<center>
	<table cellpadding="0" cellspacing="0" width="93%">
	<tr>
		<td align="left">
			<br /><br />
			<center>
			<table cellpadding="10" cellspacing="0" width="100%" class="border">
			<tr><td class="title" align="left"><?php echo $lang['RESOURCES_SEARCH']; ?></td></tr>
			<tr>
				<td align="left">
				<?php
					if(isset($_GET['type']) || isset($_GET['category']) || isset($_GET['text']) || isset($_GET['country']))
					{
				?>
					<center>
					<form action="includes/process.php" method="POST">
					<input type="hidden" name="subSearch" value="<?php echo $session->username; ?>" />
					<table cellpadding="5" cellspacing="0" width="90%">
					<tr>
						<td><center><b><?php echo $lang['RESOURCES_TYPE']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_CATEGORY']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_SEARCH_TEXT']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_COUNTRY']; ?></b></center></td>
					</tr>
					<tr>
						<td>
							<center>
							<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
							<option value="">-All Types-</option>
							<option value="Knowledge" <?php if($_GET['type']=="Knowledge"){ echo "selected=\"selected\""; } ?>>Knowledge</option>
							<option value="Ability" <?php if($_GET['type']=="Ability"){ echo "selected=\"selected\""; } ?>>Ability</option>
							<option value="Commodity" <?php if($_GET['type']=="Commodity"){ echo "selected=\"selected\""; } ?>>Commodity</option>
							<option value="Transportation" <?php if($_GET['type']=="Transportation"){ echo "selected=\"selected\""; } ?>>Transportation</option>
							<option value="Raw Materials" <?php if($_GET['type']=="Raw Materials"){ echo "selected=\"selected\""; } ?>>Raw Materials</option>
							<option value="Land" <?php if($_GET['type']=="Land"){ echo "selected=\"selected\""; } ?>>Land</option>
							</select>
							</center>
						</td>
						<td>
							<center>
							<select name="category" id="category">
							<option value="">-All Categories-</option>
							<?php 
							if(!isset($_GET['type']))
								$type = "Knowledge";
							else
								$type = $_GET['type'];
							$result = $database->query("SELECT * FROM ".TBL_CATEGORIES." WHERE type='".$type."' ORDER BY type ASC, category ASC");
							$category = mysql_fetch_array($result);
							$num_result = mysql_num_rows($result);
							for($i=0;$i<$num_result;$i++)
							{
								$name = mysql_result($result,$i,"category");
								echo "<option value=\"$name\"";
								if($name == $_GET['category'])
								{
									echo "selected=\"selected\"";
								}
								echo ">$name</option>";
							}
							?>
							</select>
							</center>
						</td>
						<td>
							<center><input type="text" name="text" size="40" maxlength="100" value="<?php echo htmlspecialchars($_GET['text']); ?>"></center>
						</td>
						<td>
							<center>
							<select name="country">
							<option value="" selected="selected">-All Countries-</option>
							<?php $countries = $database->query("SELECT * FROM ".TBL_COUNTRIES);
								$num_countries = mysql_num_rows($countries);
								for($i=0;$i<$num_countries;$i++)
								{
									$country = mysql_result($countries,$i,"country");
									$code = mysql_result($countries,$i,"code");
									echo "<option value=\"$code\" ";
									
									if(isset($_GET['country']) && $_GET['country']==$code)
										echo "selected=\"selected\" ";
									
									echo ">$country</option>";
								}
							 ?>
							</select>
							</center>
						</td>
						<td>
							<center>
							<input type="submit" value="Search" />
							</center>
						</td>
					</tr>
					</table>
					</form>
					</center>
				<?php
				}
				else
				{
				?>
					<center>
					<form action="includes/process.php" method="POST">
					<input type="hidden" name="subSearch" value="<?php echo $session->username; ?>" />
					<table cellpadding="5" cellspacing="0" width="90%">
					<tr>
						<td><center><b><?php echo $lang['RESOURCES_TYPE']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_CATEGORY']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_SEARCH_TEXT']; ?></b></center></td>
						<td><center><b><?php echo $lang['RESOURCES_COUNTRY']; ?></b></center></td>
					</tr>
					<tr>
						<td>
						<center>
						<select name="type" onChange="setOptions(this.options[this.selectedIndex].value);">
						<option value="">-All Types-</option>
						<option value="Knowledge">Knowledge</option>
						<option value="Ability">Ability</option>
						<option value="Commodity">Commodity</option>
						<option value="Transportation">Transportation</option>
						<option value="Raw Materials">Raw Materials</option>
						<option value="Land">Land</option>
						</select>
						</center>
						</td>
						<td>
						<center>
						<select name="category" id="category">
						<option value="">-All Categories-</option>
						</select>
						</center>
						</td>
						<td>
						<center>
						<input type="text" name="text" size="40" maxlength="100" />
						</center>
						</td>
						<td>
						<select name="country">
						<option value="" selected="selected">-All Countries-</option>
						<?php $countries = $database->query("SELECT * FROM ".TBL_COUNTRIES);
							$num_countries = mysql_num_rows($countries);
 							for($i=0;$i<$num_countries;$i++)
							{
								$country = mysql_result($countries,$i,"country");
								$code = mysql_result($countries,$i,"code");
								echo "<option value=\"$code\" ";
								echo ">$country</option>";
							}
						 ?>
						</select>
						</td>
						<td>
						<center>
						<input type="submit" value="Search" />
						</center>
						</td>
					</tr>
					</table>
					</form>
					</center>
				<?
				}
				?>
				</td>
			</tr>
			</table>
			</center>
			<br />
			<center>
			<table cellpadding="10" cellspacing="0" width="100%" class="border">
			<tr><td class="title" align="left"><?php echo $lang['RESOURCES_LOCATION']; ?></td></tr>
			<tr>
				<td align="left">
					<!-- Google Map -->
					<center>
					<table cellpadding="3" cellspacing="0" width="230px">
					<tr>
						<td>
						<div id="map_canvas" style="width: 900px; height: 220px;"></div>
						</td>
					</tr>
					<tr>
						<td>
						<Center><b><div id="map_name"></div></b></Center>
						</td>
					</tr>
					</table>
					</center>
				</td>
			</tr>
			</table>
			</center>
			<br />
			<center>
			<table cellpadding="10" cellspacing="0" width="100%" class="border">
			<tr><td class="title" align="left"><?php echo $lang['RESOURCES_FOUND']; ?></td></tr>
			<tr>
				<td align="left">
				<?php
					if(isset($_GET['type']) || isset($_GET['category']) || isset($_GET['text']) || isset($_GET['country']))
					{
						if($num_res<=0)
						{
							echo "<center>No resources were found under those search terms.</center>";
						}
						else
						{
							echo "<center>";
							echo "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">";
							echo "<tr><td><center><b>Type</b></center></td>";
							echo "<td><center><b>Category</b></center></td>";
							echo "<td><center><b>Name</b></center></td>";
							echo "<td><center><b>Description</b></center></td>";
							echo "<td><center><b>Country</b></center></td>";
							echo "<td><center><b>Username</b></center></td>";
							echo "</tr>";
							for($i=$start;$i<$num_res && $i<$start+$maxResources;$i++)
							{
								$type = mysql_result($res,$i,"type");
								$category = mysql_result($res,$i,"category");
								$name = mysql_result($res,$i,"title");
								$description = mysql_result($res,$i,"description");
								$country = mysql_result($res,$i,"country");
								$user = mysql_result($res,$i,"username");
								$latitude = mysql_result($res,$i,"latitude");
								$longitude = mysql_result($res,$i,"longitude");
								echo "<tr";
								if($i%2==0)
								{
									echo " bgcolor=\"#c9c9c9\"";
								}
								echo ">";
								echo "<td><center>$type</center></td>";
								echo "<td><center>$category</center></td>";
								echo "<td><center>$name</center></td>";
								echo "<td><center>$description</center></td>";
								echo "<td><center>$country</center></td>";
								echo "<td><center><a href=\"profile.php?user=$user\" title=\"View Profile\">$user</a></center>";
								
								echo "<script type=\"text/javascript\">";
								echo "addMarker($latitude, $longitude, '$name', '$user');";
								echo "</script>";
								echo "</td>";
								echo "</tr>";
							}
							echo "</table>";
							echo "</center>";
							if($num_res>$maxResources)
							{
								echo "<center>";
								echo "<br />";
								for($x=0;$x<$num_res;$x=$x+$maxResources)
								{
									if($start!=$x)
									{
										echo "<a href=\"resources.php?start=$x";
										if(isset($_GET['type']))
											echo "&type=".$_GET['type'];
										if(isset($_GET['category']))
											echo "&category=".$_GET['category'];
										if(isset($_GET['text']))
											echo "&text=".$_GET['text'];
										if(isset($_GET['country']))
											echo "&country=".$_GET['country'];
										echo "\">".(($x/$maxResources)+1)."</a>&nbsp;&nbsp;&nbsp;";
									}
									else
										echo (($x/$maxResources)+1)."&nbsp;&nbsp;&nbsp;";
								}
								echo "</center>";
							}
						}
						unset($_SESSION['searchSuccess']);	
					}
					else
					{
						displayAllResources($res);
					}
				?>
				</td>
			</tr>
			</table>
			</center>
		</td>
	</tr>
	</table>
	</center>
<?php include("footer.php"); ?>